﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Text.Json;
using System.IO;
using System.Text.Encodings.Web;
using System.Text.Unicode;

namespace Json文件操作
{
    //定义数据类
    class Book
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Author { get; set; }
        public decimal Price { get; set; }
        public string Category { get; set; }
    }

    internal class Program
    {
        static string filePath = "books.json";
        static void Main(string[] args)
        {
            //1、创建Json文件
            CreateJsonFile();
            //2、读取Json数据
            ReadJsonFile();
            //3、增加Json数据到文件
            Book book = new Book()
            {
                Id = 4,
                Name = "音乐学习",
                Author = "飞少",
                Price = 66.6m,
                Category = "技能学习"
            };
            AddDataToJson(book);
            ReadJsonFile();
            //4、修改Json数据到文件
            Book book1 = new Book()
            {
                Id = 4,
                Name = "音乐学习",
                Author = "飞少",
                Price = 100m,
                Category = "飞少牛逼！！"
            };
            InsertDataToJson(book1, 4);
            ReadJsonFile();
            //5、删除Json数据
            DeleteDataToJson(1);
            ReadJsonFile();
        }
        static List<Book> LoadJson()
        {
            //读取文件数据
            string json = File.ReadAllText(filePath);
            //对文本进行反序列化操作
            return JsonSerializer.Deserialize<List<Book>>(json);
        }
        static void SaveJson(List<Book> books)
        {
            //对象数据进行序列化操作
            JsonSerializerOptions options = new JsonSerializerOptions
            {
                WriteIndented = true,//格式化写入
                Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),//中文编码格式
            };
            string json = JsonSerializer.Serialize(books, options);
            //序列化后的文本数据写入Json文件
            File.WriteAllText(filePath, json);
        }
        static void AddDataToJson(Book book)
        {
            //1、加载Json数据
            List<Book> books = LoadJson();
            //2、数据操作
            books.Add(book);
            //3、保存Json数据
            SaveJson(books);
            Console.WriteLine("添加成功！");
        }
        static void DeleteDataToJson(int id)
        {
            //1、加载Json数据
            List<Book> books = LoadJson();
            //2、数据操作
            books.Remove(books[id - 1]);
            //3、保存Json数据
            SaveJson(books);
            Console.WriteLine("删除成功！");
        }

        static void InsertDataToJson(Book book1, int id)
        {
            //1、加载Json数据
            List<Book> books = LoadJson();
            //读取文件数据
            books[id - 1].Name = book1.Name;
            books[id - 1].Author = book1.Author;
            books[id - 1].Price = book1.Price;
            books[id - 1].Category = book1.Category;
            SaveJson(books);
            Console.WriteLine("修改成功！");
        }
        static void ReadJsonFile()
        {
            Console.WriteLine("-------------------------------------------------------------------------------");
            Console.WriteLine("------------------------------进行Json查询操作---------------------------------");
            //加载数据
            List<Book> books = LoadJson();
            //读取文件数据
            string json = File.ReadAllText(filePath);
            foreach (Book book in books)
            {
                Console.WriteLine($"id:  {book.Id}  Name:  {book.Name}" +
                    $"  Author:  {book.Author}   Price:  {book.Price}" +
                    $"  Category： {book.Category}");
            }
        }
        static void CreateJsonFile()
        {
            //对象数据进行序列化操作
            List<Book> books = new List<Book>();
            books.Add(new Book
            {
                Id = 1,
                Name = "C#程序高级应用",
                Price = 58.9m,
                Author = "张三",
                Category = "编程学习"
            });
            books.Add(new Book
            {
                Id = 2,
                Name = "设计模式",
                Price = 56.5m,
                Author = "李四",
                Category = "编程学习"
            });
            books.Add(new Book
            {
                Id = 3,
                Name = "AI学习",
                Price = 53m,
                Author = "王五",
                Category = "技能学习"
            });
            //保存
            SaveJson(books);
            Console.WriteLine("Json文件创建成功！");
        }
    }
}
